package com.ys.web.user.complain.controller;

import com.ys.utils.base.http.ResponseHandler;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.redis.aop.RedisCache;
import com.ys.web.user.complain.po.ComplainPO;
import com.ys.web.user.complain.service.ComplainService;
import com.ys.web.user.complain.vo.ComplainVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author quxinjun
 * @version 1.0.0
 * @ClassName ComplainController.java
 * @Description 用户投诉控制器
 * @createTime 2021年05月24日 10:39:00
 */
@Slf4j
@RestController
@RequestMapping("/complain")
public class ComplainController {

    @Autowired
    private ComplainService complainService;


    /**
     * @title getCauseList
     * @description 获取投诉原因列表
     * @author quxinjun
     * @updateTime 2021/5/24 11:19
     * @return: java.lang.String
     */
    @RedisCache
    @PostMapping("/getCauseList")
    public String getCauseList() {
        return ResponseHandler.list(complainService.getCauseList());
    }


    /**
     * @param uid        操作用户id
     * @param complainPO 投诉实体类
     * @title addComplain
     * @description 添加投诉
     * @author quxinjun
     * @updateTime 2021/5/24 11:48
     * @return: java.lang.String
     */
    @PostMapping("/addComplain")
    public String addComplain(@RequestAttribute Long uid, ComplainPO complainPO) {
        String msg = complainPO.validate(complainPO);
        if (StringHandler.isNotEmpty(msg)) {
            return ResponseHandler.error("500", msg);
        }
        complainPO.setUserId(uid);
        complainService.addComplain(uid,complainPO);
        //用户投诉给客服发送消息
        //参数验证
//        String errMsg = messageRequestBean.validateParam(messageRequestBean, BaseConstants.MessageConstants.MESSAGE_USER);
//        if (!StringHandler.isEmpty(errMsg)) {
//            return ResponseHandler.error(EnumType.ResponseCode.ILLEGAL.getCode(), errMsg);
//        }
//        messageService.sendMessageUser(messageRequestBean,StringHandler.getLong(uid));
        return ResponseHandler.success();
    }


    /**
     * @param uid 操作人id
     * @param id  投诉对象id
     * @title undoComplain
     * @description 撤销投诉
     * @author quxinjun
     * @updateTime 2021/5/24 14:22
     * @return: java.lang.String
     */
    @PostMapping("/undoComplain")
    public String undoComplain(@RequestAttribute Long uid, Long id) {
        if (StringHandler.isEmpty(id) || id == 0) {
            return ResponseHandler.error("500", "参数传递错误");
        }
//        complainService.undoComplain(uid, id);
        return ResponseHandler.success();
    }
//    @PostMapping("/complain/list")
    public String complainList(@RequestAttribute Long uid) {
        List<ComplainPO> list = complainService.complainList(uid);
        List<ComplainVo> vo = new ArrayList<>();
        if(StringHandler.isNotEmpty(list) && list.size()>0){
            list.forEach(o->{
                vo.add(new ComplainVo(o));
            });
        }
        return ResponseHandler.list(vo);
    }

//    @PostMapping("/complain/del")
    public String complainDel(@RequestAttribute Long uid,long complainId) {
        complainService.complainDel(uid,complainId);
        return ResponseHandler.success();
    }
}
